package com.application.lucky.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.application.lucky.Lucky;


import java.util.UUID;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author lixiaoyu
 * @version v1.0
 * @description
 * @since 2017/11/6
 */

public class PhoneSystemUtil {

    private static String IP = "";

    /**
     *
     * 目前这个调用只在 splash 赋值用，调用的请 调用 RequestCommonParams
     */
    //获取不到 imei 会用 AdVertising ID  替换  1.1.3 已被 google 提醒
    //不在获取 imei  已被 google 提醒    替换  1.2.0
    @SuppressLint("MissingPermission")
    public static String getIMEI() {
        String id = getUniquePsuedoID();
//        if (EasyPermissions.hasPermissions(Lucky.getApplication(), Manifest.permission.READ_PHONE_STATE)) {
//            TelephonyManager telephonyManager = (TelephonyManager) Lucky.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
//            if (telephonyManager != null) {
//                id = telephonyManager.getDeviceId();
//            }
//        }

//        if (TextUtils.isEmpty(id)) {
//            id = AdCache.getId();
//        }
        return id;
    }


    public static String getPhoneCountry() {
        String country = "";
        if (EasyPermissions.hasPermissions(Lucky.getApplication(), Manifest.permission.READ_PHONE_STATE)) {
            TelephonyManager telephonyManager = (TelephonyManager) Lucky.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                country = telephonyManager.getNetworkCountryIso();
            }
        }
        return country;
    }


    /**
     * 获取单个App版本号
     **/
    public static String getAppVersion() {
        String appVersion = "";
        try {
            PackageManager pManager = Lucky.getApplication().getPackageManager();
            PackageInfo packageInfo = pManager.getPackageInfo(Lucky.getApplication().getPackageName(), 0);
            appVersion = String.valueOf(packageInfo.versionCode);
        } catch (Exception e) {
        }
        return appVersion;
    }
    public static int getAppVersionCode() {
        int versionCode = 0;
        try {
            PackageManager pManager = Lucky.getApplication().getPackageManager();
            PackageInfo packageInfo = pManager.getPackageInfo(Lucky.getApplication().getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (Exception e) {
        }
        return versionCode;
    }
    /**
     * 获取单个App版本号
     **/
    public static String getAppVersionName() {
        String appVersion = "";
        try {
            PackageManager pManager = Lucky.getApplication().getPackageManager();
            PackageInfo packageInfo = pManager.getPackageInfo(Lucky.getApplication().getPackageName(), 0);
            appVersion = String.valueOf(packageInfo.versionName);
        } catch (Exception e) {
        }
        return appVersion;
    }

    public static String getIPAddress() {
//        URL infoUrl = null;
//        InputStream inStream = null;
//        String line = "";
//        try {
//            infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
//            URLConnection connection = infoUrl.openConnection();
//            HttpURLConnection httpConnection = (HttpURLConnection) connection;
//            int responseCode = httpConnection.getResponseCode();
//            if (responseCode == HttpURLConnection.HTTP_OK) {
//                inStream = httpConnection.getInputStream();
//                BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
//                StringBuilder strber = new StringBuilder();
//                while ((line = reader.readLine()) != null)
//                    strber.append(line + "\n");
//                inStream.close();
//                // 从反馈的结果中提取出IP地址
//                int start = strber.indexOf("{");
//                int end = strber.indexOf("}");
//                String json = strber.substring(start, end + 1);
//                if (json != null) {
//                    try {
//                        JSONObject jsonObject = new JSONObject(json);
//                        line = jsonObject.optString("cip");
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }
//                return line;
//            }
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return line;
        return IP;
    }

    public static void setIP(String ip) {
        IP = ip;
    }

    public static int getDeviceType() {
        //UNKNOW((short) 0), IOS((short) 100), IPAD((short) 101), ANDORID((short) 200), ANDORID_PAD((short) 202), ROBOT((short) 301), BACK((short) 401);
        return 200;
    }

    public static String getNetworkOperatorName() {
        String name = "";
        if (EasyPermissions.hasPermissions(Lucky.getApplication(), Manifest.permission.READ_PHONE_STATE)) {
            TelephonyManager telephonyManager = (TelephonyManager) Lucky.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                name = telephonyManager.getNetworkOperatorName();
            }
        }
        return name;
    }
    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return Build.MODEL;
    }

    public static String getUniquePsuedoID() {
        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            serial = Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }
}
